/**
 * 网关配置文件
 * 定义不同网关类型的硬件信息
 */

// 网关类型配置
export const GATEWAY_CONFIGS = {
    WG01: {
        name: 'WG01',
        displayName: 'WG01网关',
        hardwareInfo: {
            updateMode: 'serialport',
            ioCount: 0,
            serialPortCount: 2,
            deviceCount: 40,
            variableCount: 400,
            controlTopicCount: 5,
            uploadTopicCount: 5,
            mqttCount: 1,
            hardwareDescription: 'NBiot MQTT'
        }
    },
    WG10: {
        name: 'WG10',
        displayName: 'WG10网关',
        hardwareInfo: {
            updateMode: 'serialport',
            ioCount: 0,
            serialPortCount: 2,
            deviceCount: 40,
            variableCount: 400,
            controlTopicCount: 5,
            uploadTopicCount: 5,
            mqttCount: 1,
            hardwareDescription: '4G MQTT'
        }
    },
    WG11: {
        name: 'WG11',
        displayName: 'WG11网关',
        hardwareInfo: {
            updateMode: 'serialport',
            ioCount: 0,
            serialPortCount: 3,
            deviceCount: 60,
            variableCount: 400,
            controlTopicCount: 5,
            uploadTopicCount: 5,
            mqttCount: 1,
            hardwareDescription: '4G LORA MQTT'
        }
    },
    WG15: {
        name: 'WG15',
        displayName: 'WG15网关',
        hardwareInfo: {
            updateMode: 'serialport',
            ioCount: 0,
            serialPortCount: 3,
            deviceCount: 60,
            variableCount: 400,
            controlTopicCount: 5,
            uploadTopicCount: 5,
            mqttCount: 1,
            hardwareDescription: '4G LORA MQTT'
        }
    },
    WG20: {
        name: 'WG20',
        displayName: 'WG20网关',
        hardwareInfo: {
            updateMode: 'ethernet',
            ioCount: 0,
            serialPortCount: 3,
            deviceCount: 60,
            variableCount: 400,
            controlTopicCount: 5,
            uploadTopicCount: 5,
            mqttCount: 1,
            hardwareDescription: 'Internet MQTT'
        }
    },
    WG21: {
        name: 'WG21',
        displayName: 'WG21网关',
        hardwareInfo: {
            updateMode: 'ethernet',
            ioCount: 0,
            serialPortCount: 3,
            deviceCount: 60,
            variableCount: 800,
            controlTopicCount: 5,
            uploadTopicCount: 5,
            mqttCount: 2,
            hardwareDescription: 'Internet 4G MQTT'
        }
    },
    WG40: {
        name: 'WG40',
        displayName: 'WG40网关',
        hardwareInfo: {
            updateMode: 'ethernet',
            ioCount: 0,
            serialPortCount: 9,
            deviceCount: 120,
            variableCount: 2000,
            controlTopicCount: 5,
            uploadTopicCount: 5,
            mqttCount: 4,
            hardwareDescription: '4G Internet MQTT'
        }
    },
    WG42: {
        name: 'WG42',
        displayName: 'WG42网关',
        hardwareInfo: {
            updateMode: 'ethernet',
            ioCount: 0,
            serialPortCount: 7,
            deviceCount: 120,
            variableCount: 2000,
            controlTopicCount: 5,
            uploadTopicCount: 5,
            mqttCount: 4,
            hardwareDescription: '4G Internet MQTT'
        }
    },
    WG60: {
        name: 'WG60',
        displayName: 'WG60网关',
        hardwareInfo: {
            updateMode: 'ethernet',
            ioCount: 0,
            serialPortCount: 7,
            deviceCount: 120,
            variableCount: 2000,
            controlTopicCount: 5,
            uploadTopicCount: 5,
            mqttCount: 4,
            hardwareDescription: '5G Internet MQTT'
        }   
    },
    HJ212: {
        name: 'HJ212',
        displayName: 'HJ212网关',
        hardwareInfo: {
            updateMode: 'ethernet',
            ioCount: 0,
            serialPortCount: 9,
            deviceCount: 120,
            variableCount: 2000,
            controlTopicCount: 5,
            uploadTopicCount: 5,
            mqttCount: 4,
            hardwareDescription: '5G Internet MQTT'
        }
    }
}

/**
 * 获取网关配置
 * @param {string} gatewayType 网关类型
 * @returns {Object} 网关配置信息
 */
export function getGatewayConfig(gatewayType) {
    return GATEWAY_CONFIGS[gatewayType] || GATEWAY_CONFIGS.WG01
}

/**
 * 获取网关硬件信息
 * @param {string} gatewayType 网关类型
 * @returns {Object} 硬件信息
 */
export function getGatewayHardwareInfo(gatewayType) {
    const config = getGatewayConfig(gatewayType)
    return config.hardwareInfo
}

/**
 * 获取所有可用的网关类型
 * @returns {Array} 网关类型列表
 */
export function getAvailableGatewayTypes() {
    return Object.keys(GATEWAY_CONFIGS)
}

/**
 * 获取网关显示名称
 * @param {string} gatewayType 网关类型
 * @returns {string} 显示名称
 */
export function getGatewayDisplayName(gatewayType) {
    const config = getGatewayConfig(gatewayType)
    return config.displayName
}

/**
 * 验证网关类型是否有效
 * @param {string} gatewayType 网关类型
 * @returns {boolean} 是否有效
 */
export function isValidGatewayType(gatewayType) {
    return Object.keys(GATEWAY_CONFIGS).includes(gatewayType)
}

/**
 * 创建设备请求数据（只包含7个核心字段）
 * @param {Object} deviceData 设备数据
 * @returns {Object} 设备请求数据
 */
export function createDeviceRequestData(deviceData) {
    return {
        id: deviceData.id || -1,
        groupId: deviceData.groupId || -1,
        gatewayType: deviceData.gatewayType || 'WG01',
        name: deviceData.name || '',
        allowDeviceRename: deviceData.allowDeviceRename || false,
        allowVariableRename: deviceData.allowVariableRename || false,
        hardwareInfo: getGatewayHardwareInfo(deviceData.gatewayType || 'WG01')
    }
}

/**
 * 从后端响应数据转换为前端显示数据
 * @param {Object} responseData 后端响应数据
 * @returns {Object} 前端显示数据
 */
export function convertResponseToDisplayData(responseData) {
    return {
        id: responseData.id || -1,
        groupId: responseData.groupId || -1,
        gatewayType: responseData.gatewayType || 'WG01',
        name: responseData.name || responseData.title || responseData.name || '',
        allowDeviceRename: responseData.allowDeviceRename || false,
        allowVariableRename: responseData.allowVariableRename || false,
        // 硬件信息从网关配置中获取
        ...getGatewayHardwareInfo(responseData.gatewayType || 'WG01')
    }
}

export default {
    GATEWAY_CONFIGS,
    getGatewayConfig,
    getGatewayHardwareInfo,
    getAvailableGatewayTypes,
    getGatewayDisplayName,
    isValidGatewayType,
    createDeviceRequestData,
    convertResponseToDisplayData
}